package com.gaoyuzhe.controller;

import com.gaoyuzhe.entities.Payment;
import com.gaoyuzhe.service.PaymentService;
import com.gaoyuzhe.to.ReturnValue;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;

    @Value("${server.port}")
    private String port;
    @PostMapping("/payment/insert")
    public ReturnValue<Integer> insert(@RequestBody Payment payment) {
        int insert = paymentService.insert(payment);
        log.info("插入数据库结果：{}", insert);
        if(insert> 0){
            return new ReturnValue<>(200, "插入数据库成功,port"+port, insert);
        }
        return new ReturnValue<>(444, "插入数据库失败,port"+port, null);
    }

    @GetMapping("/payment/get/{id}")
    public ReturnValue<Payment> getPayementById(@PathVariable Long id) {
        Payment payment = paymentService.getPaymentById(id);
        if(payment ==null){
            return new ReturnValue<>(200, "获取数据不存在,port"+port, null);
        }
        return new ReturnValue<>(200, "获取数据成功,port"+port, payment);

    }
    @GetMapping("/payment/lb")
    public String lb() {
        return port;
    }

    @GetMapping("/payment/timeout")
    ReturnValue<String> timeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new ReturnValue<>(200, "获取数据成功,port"+port, port);
    }
}

